package cn.hfatec.shms.system.menu.service;

import cn.hfatec.base.BaseDao;
import cn.hfatec.shms.advice.annotation.MysqlDataSource;
import cn.hfatec.shms.constants.ShmsConstant;
import cn.hfatec.shms.enums.KeyTypeEnum;
import cn.hfatec.shms.exception.KeyGenException;
import cn.hfatec.shms.system.menu.dao.MenuDao;
import cn.hfatec.shms.system.menu.model.Menu;
import cn.hfatec.shms.system.menu.model.MenuParams;
import cn.hfatec.shms.system.menu.model.MenuView;
import cn.hfatec.shms.system.roleMenu.dao.RoleMenuDao;
import cn.hfatec.shms.utils.KeyGenUtils;
import com.google.common.collect.Lists;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.List;


/**
 * @author hfatec
 * @version 1.0
 *          <p/>
 *          the JAVA code is generate by middlegen
 */
@Service("MenuService")
public class MenuService {

    public static final String BEAN_ID = "MenuService";

    //注入Dao层
    @Resource(name = "MenuDao")
    private MenuDao menuDao;

    //注入BaseDao
    @Resource(name = "BaseDao")
    private BaseDao baseDao;
    //注入roleMenuDao
    @Resource(name = "RoleMenuDao")
    private RoleMenuDao roleMenuDao;

    /**
     * 查询数据
     *
     * @param menuParams
     */
    @Transactional
    @MysqlDataSource
    public List queryMenuTable(MenuParams menuParams) {

        HashMap<String, Object> params = new HashMap<String, Object>();
        params.put("params", menuParams);
        return baseDao.queryList("queryMenu", params);
    }

    /**
     * 插入数据
     *
     * @param menu
     */
    @Transactional
    @MysqlDataSource
    public void saveMenu(Menu menu) throws KeyGenException {
        if (menu.getId() != null) {
            Menu result = getMenu(menu.getId());
            if (result != null) {
                menuDao.update(menu);
            } else {
                menu.setMenuId(KeyGenUtils.createKeyByType(KeyTypeEnum.MENU_KEY));
                menuDao.insert(menu);
            }
        } else {
            menu.setMenuId(KeyGenUtils.createKeyByType(KeyTypeEnum.MENU_KEY));
            menuDao.insert(menu);
        }
    }

    /**
     * 删除数据
     *
     * @param id
     */
    @Transactional
    @MysqlDataSource
    public void deleteMenu(Integer id, String menuId) {
        roleMenuDao.deleteRoleMenuForMenuId(menuId);
        menuDao.delete(id);
    }


    /**
     * 根据ID获取数据
     *
     * @param id
     */
    @Transactional
    @MysqlDataSource
    public Menu getMenu(Integer id) {

        return menuDao.getMenuById(id);
    }

    /**
     * 根据ID获取数据
     *
     * @param menuId
     */
    @Transactional
    @MysqlDataSource
    public Menu getMenu(String menuId) {

        return menuDao.getMenuByMenuId(menuId);
    }

    /**
     * 查询所有的菜单数据
     *
     * @return
     * @throws java.lang.reflect.InvocationTargetException
     * @throws IllegalAccessException
     */
    @Transactional
    @MysqlDataSource
    public List<MenuView> queryAllMenuList() throws InvocationTargetException, IllegalAccessException {
        //查询所有已分配的菜单信息
        MenuParams menuParams = new MenuParams();
        List<Menu> allMenuList = menuDao.queryMenu(menuParams);
        return getMenuList(ShmsConstant.ROOT_MENU_ID, allMenuList);
    }

    /**
     * 根据登录用户
     *
     * @param loginName
     * @return
     */
    @Transactional
    @MysqlDataSource
    public List<MenuView> queryMenuListByLoginName(String loginName) throws InvocationTargetException, IllegalAccessException {
        //查询所有已分配的菜单信息
        List<Menu> allMenuList = menuDao.queryMenuListByLoginName(loginName);
        return getMenuList(ShmsConstant.ROOT_MENU_ID, allMenuList);
    }

    /**
     * 根据父节点ID、总菜单数据，递归获取菜单视图信息
     *
     * @param parentMenuId
     * @param allMenuList
     * @return
     * @throws InvocationTargetException
     * @throws IllegalAccessException
     */
    @MysqlDataSource
    public List<MenuView> getMenuList(String parentMenuId, List<Menu> allMenuList) throws InvocationTargetException, IllegalAccessException {
        List<MenuView> list = Lists.newArrayList();
        for (Menu menu : allMenuList) {
            if (menu.getParentMenuId() != null && menu.getParentMenuId().equals(parentMenuId)) {
                MenuView menuView = new MenuView();
                BeanUtils.copyProperties(menuView, menu);
                menuView.setChildrenMenuList(getMenuList(menuView.getMenuId(), allMenuList));
                list.add(menuView);
            }
        }
        return list;
    }
}